package com.lj.api.filter;


import com.lj.api.auth.JwtTools;
import com.lj.api.auth.exce.IllegalAccessTokenException;
import com.lj.api.auth.exce.OverPeriodException;
import com.lj.api.common.CodeMsg;
import com.lj.api.common.Result;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

@Component
public class RequestFilter implements Filter {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Content-Type,access_token");
        response.setHeader("Access-Control-Expose-Headers", "*");
        response.setHeader("Access-Control-Request-Headers", "Origin, X-Requested-With, content-Type, Accept, access_token");

        System.out.println(request.getMethod().toUpperCase());

        if (request.getMethod().toUpperCase().equals("OPTIONS")){
            return;
        }



        String url = request.getRequestURL().toString();
        System.out.println("请求进来 == "+url);
        if (url.contains("/json/")){
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        for(int i=0;i<URLWhiteList.WHITE_LIST.length;i++){
            if (url.contains(URLWhiteList.WHITE_LIST[i])){
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
        }

        //token验证处理
        boolean pass = false;
       String token = request.getHeader("access_token");
        String errorMsg = null;
        try {
            pass =JwtTools.checkToken(token);
        }catch (IllegalAccessTokenException e){
            errorMsg =Result.fail(CodeMsg.ILLEGAL_JTW);
        }catch (OverPeriodException e){
            errorMsg =Result.fail(CodeMsg.OUTOFDATE_JTW);
        }catch (Exception e){
            e.printStackTrace();
            errorMsg =Result.fail(CodeMsg.ILLEGAL_JTW);
        }

        System.out.println(url +" 处理结果："+ pass);
        if (pass) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        PrintWriter writer = response.getWriter();
        writer.write(errorMsg);
        response.getWriter().flush();
        writer.close();
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
